<template>
  <div>
    <!-- 表单 -->
    <el-form size="small" :model="form" :inline="true">
      <el-form-item label="校 区" style="font-weight:bold;">
        <el-select v-model="form.campusId" placeholder="请选择校区" style="width:150px;" clearable>
          <el-option v-for="item in CampusName" :key="item.id" :label="item.name" :value="item.id"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="楼 宇" style="font-weight:bold;">
        <el-select v-model="form.buildId" placeholder="请选择楼宇" style="width:150px;" clearable>
          <el-option v-for="item in BuildingName" :key="item.id" :label="item.name" :value="item.id"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="楼 长" style="font-weight:bold;">
        <el-select v-model="form.adminId" placeholder="请选择楼长" style="width:150px;" clearable>
          <el-option v-for="item in AdminName" :key="item.id" :label="item.name" :value="item.id"></el-option>
        </el-select>
      </el-form-item>
      <!-- <el-form-item label="男女宿舍 :">
            <el-select v-model="form.buildingType" placeholder="请选择" >
              <el-option label="男" value="1"></el-option>
              <el-option label="女" value="0"></el-option>
            </el-select>
          </el-form-item> -->
      <el-form-item label="楼宇类型" style="font-weight:bold;">
        <el-select v-model="form.buildingType" placeholder="请选择楼宇类型" style="width:150px" clearable>
          <el-option label="女生楼" value="0"></el-option>
          <el-option label="男生楼" value="1"></el-option>
          <el-option label="混合楼" value="2"></el-option>
          <el-option label="非宿舍楼" value="3"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="楼 高" style="font-weight:bold;">
        <el-input v-model="form.height" placeholder="请输入楼高" style="width:150px;" clearable></el-input>
      </el-form-item>
    </el-form>
    <!-- 表单 -->

    <!-- 按钮 -->
    <div class="buttonList">
      <el-button class="addBtn" size="mini" type="primary" @click="dialog_newly = true"
        icon="el-icon-folder-add">新增</el-button>
      <!-- <el-button size="small" type="success" icon="el-icon-download" @click="downLoadBuilding" plain>下载模板</el-button>
          <el-upload class="upload-button" action="#" :before-upload="beforeUpload" ref="newupload" :show-file-list="false"
            :http-request="uploadHttpRequest" :limit="3" :file-list="fileList">
            <el-button size="small" type="primary" plain icon="el-icon-upload2">上传楼宇信息</el-button>
          </el-upload>
          <el-button size="small" type="success" plain icon="el-icon-download" @click="exportBuilding"
            class="export-button">导出楼宇信息</el-button> -->
      <div>
        <el-button size="small" class="search_button" type="primary" icon="el-icon-search"
          @click="getDataList">查询</el-button>
        <el-button size="small" class="reset_button" icon="el-icon-refresh" @click="reset">重置</el-button>
      </div>
    </div>
    <!-- 按钮 -->

    <el-divider></el-divider>

    <!-- 表格 -->
    <el-table size="medium" ref="multipleTable" :data="tableData" border style="width: 100%"
      @selection-change="handleSelectionChange">
      <!-- <el-table-column type="selection" width="50" align="center"></el-table-column> -->
      <!-- <el-table-column prop="id" label="楼宇ID" align="center"></el-table-column> -->
      <el-table-column prop="campusName" label="校区" align="center"></el-table-column>
      <el-table-column prop="name" label="楼宇" align="center"></el-table-column>
      <el-table-column prop="type" label="楼宇类型" align="center">
        <template slot-scope="scope">
          <span v-if="scope.row.buildingType == 0">女生楼</span>
          <span v-if="scope.row.buildingType == 1">男生楼</span>
          <span v-if="scope.row.buildingType == 2">混合楼</span>
          <span v-if="scope.row.buildingType == 3">非宿舍楼</span>
        </template>
      </el-table-column>
      <el-table-column prop="height" label="楼高" align="center"></el-table-column>
      <el-table-column prop="userName" label="楼长工号" align="center"></el-table-column>
      <el-table-column prop="adminName" label="楼长姓名" align="center"></el-table-column>
      <!-- <el-table-column prop="buildType" label="男女宿舍" align="center">
            <template slot-scope="scope">
              <span v-if="scope.row.buildType == 0">女</span>
              <span v-if="scope.row.buildType == 1">男</span>
            </template>
          </el-table-column> -->
      <el-table-column prop="sort" label="楼宇排序" align="center"></el-table-column>
      <el-table-column prop="state" label="状态" align="center">
        <template slot-scope="scope">
          <span v-if="scope.row.state == 0" style="color:#67C23A;">正常</span>
          <span v-if="scope.row.state == 1" style="color:red;">停用</span>
        </template>
      </el-table-column>
      <el-table-column prop="notes" label="楼宇备注" align="center"></el-table-column>
      <el-table-column prop="handle" label="操作" align="center" width="160">
        <template slot-scope="scope">
          <el-button size="mini" type="primary" plain @click="updateBuilding(scope.row)">修改</el-button>
          <el-button size="mini" type="danger" plain @click="deleteBuilding({ id: scope.row.id })">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 表格 -->

    <el-pagination class="pagination" background layout="total, sizes, prev, pager, next, jumper" :total="total"
      :page-size="10" :page-sizes="[10, 20, 30, 40]" @current-change="handleCurrentChange"
      @size-change="handleSizeChange"></el-pagination>

    <!-- 添加楼宇弹窗 -->
    <el-dialog title="添加楼宇" :visible.sync="dialog_newly">
      <el-form :model="formNew" :label-position="labelPosition" label-width="100px" style="margin-left:20%;">
        <el-form-item label="校区" style="font-weight:bold;">
          <el-select v-model="formNew.campusId" placeholder="请选择校区" style="width:60%" clearable>
            <el-option v-for="item in CampusName" :key="item.id" :label="item.name" :value="item.id"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="楼长" style="font-weight:bold;">
          <el-select v-model="formNew.adminId" placeholder="请选择楼长" style="width:60%" clearable>
            <el-option v-for="item in AdminName" :key="item.id" :label="item.name" :value="item.id"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="楼宇类型" style="font-weight:bold;">
          <el-select v-model="formNew.type" placeholder="请选择楼宇类型" style="width:60%" clearable>
            <el-option label="女生楼" value="0"></el-option>
            <el-option label="男生楼" value="1"></el-option>
            <el-option label="混合楼" value="2"></el-option>
            <el-option label="非宿舍楼" value="3"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="楼高" style="font-weight:bold;">
          <el-input v-model="formNew.height" placeholder="请输入楼高(例如10)" style="width:60%;" clearable></el-input>
        </el-form-item>
        <el-form-item label="楼宇名" style="font-weight:bold;">
          <el-input v-model="formNew.name" placeholder="请输入楼宇名" style="width:60%;" clearable></el-input>
        </el-form-item>
        <el-form-item label="楼宇备注 :">
          <el-input type="textarea" v-model="formNew.notes" style="width:60%" placeholder="请输入备注" clearable></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialog_newly = false">取 消</el-button>
        <el-button type="primary" @click="addBuilding" class="dialogbutton">确 定</el-button>
      </div>
    </el-dialog>
    <!-- 添加校区信息弹窗 -->


    <!-- 修改楼宇信息弹窗 -->
    <el-dialog title="修改楼宇信息" :visible.sync="dialog_change">
      <el-form :model="formChange" :label-position="labelPosition" label-width="100px" style="margin-left:20%;">
        <el-form-item label="校区 :">
          <el-select v-model="formChange.campusId" placeholder="请选择校区" style="width:60%" clearable>
            <el-option v-for="item in CampusName" :key="item.id" :label="item.name" :value="item.id"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="楼长 :">
          <el-select v-model="formChange.adminId" placeholder="请选择楼长" style="width:60%" clearable>
            <el-option v-for="item in AdminName" :key="item.id" :label="item.name" :value="item.id"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="楼宇名 :">
          <el-input v-model="formChange.buildName" style="width:60%" placeholder="请输入楼宇名" clearable></el-input>
          <!-- <el-select v-model="formChange.buildId" placeholder="请选择楼宇名" style="width:60%">
                <el-option v-for="item in BuildingName" :key="item.id" :label="item.name" :value="item.id"></el-option>
              </el-select> -->
        </el-form-item>
        <el-form-item label="楼宇类型">
          <el-select v-model="formChange.type" placeholder="请选择楼宇类型" style="width:60%" clearable>
            <el-option label="女生楼" value="0"></el-option>
            <el-option label="男生楼" value="1"></el-option>
            <el-option label="混合楼" value="2"></el-option>
            <el-option label="非宿舍楼" value="3"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="楼高 :">
          <el-input v-model="formChange.height" style="width:60%" placeholder="请输入楼高" clearable></el-input>
        </el-form-item>
        <!-- <el-form-item label="男女宿舍 :" >
              <el-select v-model="formChange.buildTyp" placeholder="请选择男女宿舍" style="width:60%">
                <el-option label="女" value="0"></el-option>
                <el-option label="男" value="1"></el-option>
              </el-select>
            </el-form-item> -->
        <el-form-item label="状态 :">
          <el-select v-model="formChange.state" placeholder="请选择状态" style="width:60%" clearable>
            <el-option label="正常" value="0"></el-option>
            <el-option label="停用" value="1"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="楼宇排序 :">
          <el-input v-model="formChange.sort" style="width:60%" placeholder="请输入排序" clearable></el-input>
        </el-form-item>
        <el-form-item label="楼宇备注 :">
          <el-input type="textarea" v-model="formChange.notes" style="width:60%" placeholder="请输入备注" clearable></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialog_change = false">取 消</el-button>
        <el-button type="primary" @click="updateBuildingConfirm" class="dialogbutton">确 定</el-button>
      </div>
    </el-dialog>
    <!-- 修改楼宇信息弹窗 -->

  </div>
</template>

<script>
import {
  getCampusName,
  getAdminName,
  getBuildingName,
  getBuildingList,
  updateBuilding,
  deleteBuilding,
  exportBuilding,
  uploadBuilding,
  downLoadBuilding,
  addBuilding
} from '_api/DormManage/Building/index.js'
export default {
  data() {
    return {
      labelPosition: "right",
      dialog_change: false,
      dialog_newly: false,
      form: {
        adminId: "",
        buildId: "",
        type: "",
        campusId: "",
        current: 0,
        field: "",
        height: "",
        order: "",
        pageSize: 10
      },
      formNew: {

      },
      formChange: {
        adminId: "",
        type: "",
        buildName: "",
        campusId: "",
        height: "",
        id: "",
        state: "",
        sort: "",
        notes: ""
      },
      tableData: [],
      CampusName: [],
      AdminName: [],
      BuildingName: [],
      timeNow: "",
      fileList: [],
      total: 0
    }
  },
  watch: {
    form: {
      // 执行方法
      handler(oldValue, newValue) {
        getBuildingName({ campusId: newValue.campusId, type: 0 }).then(res => {
          if (res.status == 200) {
            this.BuildingName = res.data.data
          }
        })
      },
      deep: true, // 深度监听
      // immediate: true  // 第一次改变就执行
    }
  },
  methods: {
    handleSelectionChange() {

    },
    //重置
    reset() {
      this.form = {
        adminId: "",
        buildId: "",
        type: "",
        campusId: "",
        current: 0,
        field: "",
        height: "",
        order: "",
        pageSize: 10
      }
      this.getBuildingList()
    },
    //新增楼宇
    addBuilding() {
      addBuilding(this.formNew).then(res => {
        if (res.data.success == true) {
          this.dialog_newly = false
          this.$message({
            message: '添加成功!',
            duration: 1000,
            type: 'success',
            onClose: () => {
              this.reset()
            }
          })
        } else {
          this.$message.info(res.data.message)
        }
      }).catch(error => {
        // console.log(error.response)
        if (error.response.status != 200) {
          this.$message.info(error.response.data.message)
        }
      })
    },
    // 导入楼宇信息
    uploadHttpRequest(data) {
      console.log(data)
      let fd = new FormData()
      fd.append('file', data.file)
      uploadBuilding(fd).then(res => {
        console.log(res)
        if (res.data.success == true) {
          this.$message.success("导入成功！")
          this.reset()
        } else {
          this.$message.info(res.data.message)
        }
      })

    },
    beforeUpload(file) {
      // console.log(file)
      // 校验
      let legalName = ["xlsx", "xls"];
      // 拿到后缀名
      let name = file.name.substring(
        file.name.lastIndexOf(".") + 1,
        file.name.length
      );
      if (legalName.includes(name)) {
        // console.log(legalName.includes(name));
      } else {
        this.$message.info("文件格式不对,仅限xls和xlsx");
        return false;
      }
    },
    // 下载楼宇信息模板
    downLoadBuilding() {
      downLoadBuilding().then(res => {
        // console.log(res, "121212121212")
        const link = document.createElement('a')  //创建a标签
        const blob = new Blob([res.data], { type: 'application/vnd.ms-excel' }) //设置文件流
        link.style.display = 'none'// 设置连接
        link.href = URL.createObjectURL(blob)  //将文件流转化为blob地址
        this.getCurrentTime()
        link.download = '楼宇信息模板_' + this.timeNow
        document.body.appendChild(link) // 模拟点击事件
        link.click()  //设置点击事件
        this.$message.success('下载成功！')
      })
    },
    getCurrentTime() {
      //获取当前时间并打印
      var _this = this;
      let yy = new Date().getFullYear();
      let mm = new Date().getMonth() + 1;
      let dd = new Date().getDate();
      let hh = new Date().getHours();
      let mf = new Date().getMinutes() < 10 ? '0' + new Date().getMinutes() : new Date().getMinutes();
      let ss = new Date().getSeconds() < 10 ? '0' + new Date().getSeconds() : new Date().getSeconds();
      _this.gettime = yy + '/' + mm + '/' + dd + ' ' + hh + ':' + mf + ':' + ss;
      console.log(_this.gettime)
      this.timeNow = _this.gettime
    },
    // 导出楼宇信息
    exportBuilding() {
      exportBuilding(this.form).then(res => {
        // console.log(res.data,"121212121")
        const link = document.createElement('a')  //创建a标签
        const blob = new Blob([res.data], { type: 'application/vnd.ms-excel' }) //设置文件流
        link.style.display = 'none'// 设置连接
        link.href = URL.createObjectURL(blob)  //将文件流转化为blob地址
        this.getCurrentTime()
        link.download = '楼宇信息报表_' + this.timeNow
        document.body.appendChild(link) // 模拟点击事件
        link.click()  //设置点击事件
        this.$message.success('导出成功！')
      })
    },
    // 删除楼宇
    deleteBuilding(e) {
      this.$confirm('此操作将删除该楼宇, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        deleteBuilding(e).then(res => {
          console.log(res)
          if (res.status == 200) {
            this.$message({
              message: '删除成功!',
              duration: 1000,
              type: 'success',
              onClose: () => {
                this.getBuildingListHandle()
              }
            })
          }
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    // 修改楼宇弹窗
    updateBuilding(e) {
      // console.log(e)
      this.dialog_change = true
      this.AdminName.forEach(item => {
        if (item.name == e.adminName) {
          this.formChange.adminId = item.id
        }
      })
      this.CampusName.forEach(item => {
        if (item.name == e.campusName) {
          this.formChange.campusId = item.id
        }
      })
      // this.BuildingName.forEach(item => {
      //   if (item.name == e.name) {
      //     this.formChange.buildId = item.id
      //   }
      // })
      this.formChange.buildName = e.name
      this.formChange.height = e.height
      this.formChange.type = e.buildingType
      this.formChange.height = this.formChange.height.toString()
      this.formChange.id = e.id
      this.formChange.state = e.state
      this.formChange.sort = e.sort
      this.formChange.notes = e.notes
      // console.log(this.formChange)
    },
    // 修改楼宇确认
    updateBuildingConfirm() {
      this.BuildingName.forEach(item => {
        if (item.name == this.formChange.buildId) {
          this.formChange.buildId = item.id
        }
      })
      updateBuilding(this.formChange).then(res => {
        if (res.data.success == true) {
          this.dialog_change = false
          this.$message({
            message: '修改成功!',
            duration: 1000,
            type: 'success',
            onClose: () => {
              this.getBuildingListHandle()
            }
          })
        } else {
          this.$message.info(res.data.message)
        }
      })

    },
    // prev(e) {
    //   //上一页
    //   this.form.current = e-1
    //   this.getBuildingList()
    // },
    // next(e) {
    //   //下一页
    //   this.form.current = e-1
    //   this.getBuildingList()
    // },
    handleCurrentChange(e) {
      //当前页
      this.form.current = e
      this.getBuildingList()
    },
    handleSizeChange(e) {
      this.form.pageSize = e
      this.getBuildingList()
    },
    // 查询按钮
    getDataList() {
      this.form.current = 0
      this.getBuildingList()
    },
    // 查询楼宇-操作后
    getBuildingListHandle() {
      getBuildingList(
        this.form
      ).then(res => {
        if (res.data.success == true) {
          this.tableData = res.data.data.list
          this.total = res.data.data.total
        } else {
          this.tableData = res.data.data
          this.total = ""
          this.$message.info(res.data.message)
        }
      })
    },
    // 查询楼宇
    getBuildingList() {
      getBuildingList(
        this.form
      ).then(res => {
        // console.log(res)
        if (res.data.success == true) {
          this.getCampusName()
          this.getAdminName()
          this.getBuildingName()
          this.tableData = res.data.data.list
          this.total = res.data.data.total
        } else {
          this.tableData = res.data.data
          this.total = ""
          this.$message.info(res.data.message)
        }
      })
    },
    //查询校区名称
    getCampusName() {
      getCampusName().then(res => {
        // console.log(res)
        if (res.status == 200) {
          this.CampusName = res.data.data
        }
      })
    },
    //获取楼长姓名
    getAdminName() {
      getAdminName().then(res => {
        // console.log(res)
        if (res.status == 200) {
          this.AdminName = res.data.data
        }
      })
    },
    //获取楼宇名
    getBuildingName() {
      getBuildingName({ type: 1 }).then(res => {
        // console.log(res)
        if (res.status == 200) {
          this.BuildingName = res.data.data
        }
      })
    },

  },
  created() {
    this.getBuildingList()
    // this.getCampusName()
    // this.getAdminName()
    // this.getBuildingName()
  },
};
</script>
<style lang="scss" scoped>
.addBtn {
  margin-left: 44px;
}

.search_button {
  margin-left: 10px;
}

.reset_button {
  margin-left: 10px;
}

// .delete_button{
//   margin-left: 40px;
// }
.upload-button {
  margin-left: 10px;
}

.export-button {
  margin-left: 10px;
}

.dialogbutton {
  margin-right: 40%;
}

.buttonList {
  display: flex;
  width: 70%;
  justify-content: space-between;
}

.pagination {
  float: right;
  margin-top: 10px;
}
</style>
